package com.dao.Impl;

import com.dao.RoleDao;
import com.entity.News;
import com.entity.PageBean;
import com.entity.Role;
import com.entity.User;
import com.util.JDBC;
import com.util.TransBean;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Role> findAll() {
        List<List> lists = JDBC.selectArray("select id,roleName,grade,description,status from role");
        if (lists != null) {
            List<Role> rlist = new ArrayList<>();
            for (List list : lists) {
                Role role = new Role();
                role.setId((Integer) list.get(0));
                role.setRoleName((String) list.get(1));
                role.setGrade((Integer) list.get(2));
                role.setDescription((String) list.get(3));
                role.setStatus((Integer) list.get(4));
                rlist.add(role);
            }
            return rlist;
        }
        return null;
    }

    @Override
    public void findAllPege(PageBean pageBean) {
        String sql = "select count(*) from role where 1=1 %s ";
        String[] factor = pageBean.getFactor();
        String whereStr = "";
        if(factor!=null){
            for (int i = 0; i < factor.length; i++) {
                whereStr+=" and "+factor[i];
            }
        }
        sql = String.format(sql,whereStr);
        List<List> lists = JDBC.selectArray(sql);
        Integer count=null;
        if(lists!=null){
            count= ((Long) lists.get(0).get(0)).intValue();
        }
        sql = "select id,roleName,grade,description,status from role where 1=1 %s limit ?,?";
        sql = String.format(sql,whereStr);
        List<Map> maps = JDBC.selectMap(sql,(pageBean.getPageNo() - 1) * pageBean.getPageSize(),pageBean.getPageSize());
        pageBean.setData(TransBean.populate(Role.class,maps));
        if(count!=null){
            pageBean.setRowCount(count);
        }else{
            pageBean.setRowCount(0);
        }
    }

    @Override
    public int addRole(Role role) {
        return JDBC.sqlInsert("insert into role(roleName,grade,description,status) values(?,?,?,?)",
                role.getRoleName(),1,role.getDescription(),role.getStatus());
    }

    @Override
    public Role findByName(String name) {
        List<Role> list = TransBean.populate(Role.class,
                JDBC.selectMap("select id,roleName,grade,description,status from role where roleName=?", name));
        if(list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public int deleteRole(String name) {
        return JDBC.sqlUpdate("delete from role where roleName=?",name);
    }

    @Override
    public int updateRole(Role role) {
        return JDBC.sqlUpdate("update role set roleName=?,description=?,status=? where id=?",
                role.getRoleName(),role.getDescription(),role.getStatus(),role.getId());
    }
}
